package com.cobo.fitness.core;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.FileInputStream;

public class OpfUtils {

    public static Opf invoke(String fp) {
        Opf opf=null;
        if (!new File(fp).exists()) {
            return opf;
        }
        try (FileInputStream fis = new FileInputStream(fp)){
            // 创建一个SAXReader实例，用于读取XML文件
            SAXReader reader = new SAXReader();
            // 读取XML文件，返回Document对象
            Document document = reader.read(fis);

            // 获取根元素
            Element rootElement = document.getRootElement();
            System.out.println("Root element: " + rootElement.getName());

            // 遍历根元素下的所有子元素
            for (Object obj : rootElement.elements()) {
                Element element = (Element) obj;
                if (element.getName().equalsIgnoreCase("metadata")) {
                    Element title = element.element("title");

                    Element language = element.element("language");
                    opf = Opf.builder().title(title.getText())
                            .language(language.getText())
                            .build();
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return opf;
    }

    public static void main(String[] args)throws Exception {
        invoke("d://metadata.opf");
    }
}
